<?php
abstract class ZeusLib_Cache_Abstract
{

	/**
	 * Cache xml paths for the cache settings
	 *
	 * @var string
	 */
	const ZEUS_CACHE_XML_PATH_ACTIVE   = 'config/global/cache/active';
	const ZEUS_CACHE_XML_PATH_LIFETIME = 'config/global/cache/lifetime';

	/**
	 * Object is cached flag
	 *
	 * @var boolean
	 */
	private $_isCached = false;

	/**
	 * Cache name with that the cache can be loaded and saved
	 *
	 * @var string
	 */
	private $_cacheName;


	/**
	 * Constructor to set the $CACHE_FILE as the cache name to automatically load the cache for an specific class.
	 */
	public function __construct()
	{
		// Check whether a cache filename is given
		if(isset($this->CACHE_FILE))
		{
			// Set the cache name to load the cache
			$this->setCacheName($this->CACHE_FILE);
		}

		// Check whether the cache param is given
		if(isset($this->CACHE_ACTIVE) && $this->CACHE_ACTIVE)
		{
			// Set the cache name to load for the class
			$this->setCacheName(get_class($this));
		}
	}

	/**
	 * Destructor to save the cache whenever the file doesn't exist an the cache is active.
	 */
	public function __destruct()
	{
		$this->_saveCache();
	}

	/**
	 * Sets the cache name and loads the cache if it exists.
	 *
	 * @param string $cacheName
	 */
	public function setCacheName($cacheName)
	{
		// Set the cache name
		$this->_cacheName = md5($cacheName);

		// Load the cache
		$this->_loadCache();
	}

	/**
	 * Loads the cache if it exists.
	 */
	protected function _loadCache()
	{
		// Check whether the cache is active, and the may given file exists
		if($this->_cacheName !== null && file_exists(PATH_VAR_CACHE.$this->_cacheName))
		{
			// Set the object is cached
			$this->_isCached = true;

			// Get the file data
			$data = ZeusLib_Helper_File::readFile(PATH_VAR_CACHE.$this->_cacheName);

			// Get the unserialized data
			$unserializedData = $this->_unserialize($data);

			// Set current data to last serialized data
			$this->setData($unserializedData);
		}
	}

	/**
	 * Saves the cache if it doens't exist.
	 */
	protected function _saveCache()
	{
		// Check whether the cache is active, and the may given file not exists
		if($this->_cacheName !== null && Zeus::getConfig(self::ZEUS_CACHE_XML_PATH_ACTIVE)->asBoolean())
		{
			// Check whether the file not exists. Use the absolute path, because php changes the workpath when destructing
			if(!file_exists(PATH_VAR_CACHE.$this->_cacheName))
			{
				// Get the serialized data
				$serializedData = $this->_serialize();

				// Write the data into the file
				ZeusLib_Helper_File::writeFile(PATH_VAR_CACHE.$this->_cacheName, $serializedData);
			}
		}
	}

	/**
	 * Return whether the object is cached or not.
	 *
	 * @return boolean
	 */
	public function isCached()
	{
		return $this->_isCached;
	}

	/**
	 * Used to serialize objects.
	 */
	abstract protected function _serialize();

	/**
	 * Used to deserialize data.
	 *
	 * @param string $str
	 */
	abstract protected function _unserialize($str);

}